package com.beiding.sqlbuilder.impl;

import com.beiding.sqlbuilder.*;

public class SimpleSelectBuilder implements SelectBuilder {

    private QueryBuilder where;

    private QueryBuilder on;

    private QueryBuilder having;

    private ReturnBuilder select;


    public SimpleSelectBuilder where(QueryBuilder where) {
        this.where = where;
        return this;
    }

    public SimpleSelectBuilder where(Q.OuterQ outerQ) {
        return this.andWhere(outerQ);
    }

    public SimpleSelectBuilder andWhere(Q.OuterQ outerQ) {
        return this.where(Q.and(outerQ));
    }

    public SimpleSelectBuilder orWhere(Q.OuterQ outerQ) {
        return this.where(Q.or(outerQ));
    }


    public SimpleSelectBuilder on(QueryBuilder on) {
        this.on = on;
        return this;
    }


    public SimpleSelectBuilder on(Q.OuterQ outerQ) {
        return this.andOn(outerQ);
    }

    public SimpleSelectBuilder andOn(Q.OuterQ outerQ) {
        return this.on(Q.and(outerQ));
    }

    public SimpleSelectBuilder orOn(Q.OuterQ outerQ) {
        return this.on(Q.or(outerQ));
    }

    public SimpleSelectBuilder having(QueryBuilder having) {
        this.having = having;
        return this;
    }


    public SimpleSelectBuilder having(Q.OuterQ outerQ) {
        return this.andHaving(outerQ);
    }

    public SimpleSelectBuilder andHaving(Q.OuterQ outerQ) {
        return this.having(Q.and(outerQ));
    }

    public SimpleSelectBuilder orHaving(Q.OuterQ outerQ) {
        return this.having(Q.or(outerQ));
    }

    SimpleSelectBuilder select(ReturnBuilder select) {
        this.select = select;
        return this;
    }

    private BuilderContext builderContext;

    @Override
    public void setContext(BuilderContext context) {
        this.builderContext = context;
    }

    @Override
    public BuilderContext getContext() {
        return builderContext;
    }

    @Override
    public Sentence build() {

        Tables tableHolder = new Tables();

        String sl = null;
        if (select != null) {
            sl = select.build(builderContext);
            tableHolder.addTables(select.getTables());
        }

        String wh = null;

        if (where != null) {
            where.setContext(builderContext);
            wh = where.build();
            tableHolder.addTables(where.getTables());
        }

        String o = null;

        if (on != null) {
            on.setContext(builderContext);
            o = on.build();
            tableHolder.addTables(on.getTables());
        }

        String hv = null;
        if (having != null) {
            having.setContext(builderContext);
            hv = having.build();
            tableHolder.addTables(having.getTables());
        }

        String fr = null;

        TableHandler tableHandler = builderContext.getTableHandler();

        fr = tableHandler.handle(tableHolder.getTables());

        return new SimpleSentence(builderContext.getSelectHandler().handle(sl, fr, wh, o, hv));
    }


}
